<?php
$x=4;
$a = array();
$n = 5;
for ($i=1; $i<$n; $i++)
	$a[$i] = array();



$c = 1;
for ( $col = 0; $col < $n; $col += 2) {
	$col2 = $col;
	$row = $n-1;
	while ($row >= 0 && $col2 >= 0) {
		$a[$row][$col2] = $c++;
		$row--; 
		$col2--;
	}
}

for ($row=$n-2;$row>=0; $row -= 2) {
	$row2 = $row;
	$col = 0;
	while ( $row2 < $n && $col < $n ) {
		$a[$row2][$col] = $c++;
		$row2++;
		$col++;		
	}
}

for ($col=1; $col<$n; $col+=2 ) {
	$col2 = $col;
	$row = 0;
	while ( $row < $n && $col2 < $n ) {
		$a[$row][$col2] = $c++;
		$row++;
		$col2++;
	}
} 

for ($row=$n-3; $row >= 0; $row -= 2 ) {
	$row2 = $row;
	$col = $n-1;
	while ($row2 >= 0 && $col >= 0 ) {
		$a[$row2][$col] = $c++;
		$row2--;
		$col--;
	}
}

?>